package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.service.SmsService;
import com.nineclock.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    AliyunSmsUtils aliyunSmsUtils;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void createSmsCode(String mobile, String type) {
        //健壮性判断，参数不能为空
        if (mobile == null || type == null){
            throw new NcException(ResponseEnum.MESSAGE_PARAMS_LOST);
        }
        //判断手机号规则(中国地区)
        if (!PhoneNumCheckUtils.isChinaPhoneLegal(mobile)){
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        //判断发送验证码的类型  是否是注册   如果已注册则不允许注册
        Integer count = sysUserService.queryUserForMobile(mobile);
        if(type.equals(SMSConstant.SMS_TYPE_REGISTER) ){
            if (count>0){
                throw new NcException(ResponseEnum.USER_MOBILE_EMAIL_EXISTS);
            }
        }else {
            if (count == 0){
                throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
            }
        }
    //生成随机验证码
        String randomCode = RandomUtil.randomNumbers(6);

//        //发送短信验证码
//        Boolean result = aliyunSmsUtils.sendSMS(mobile, randomCode);
//        if (!result){
//            throw new NcException(ResponseEnum.CODE_IMAGE_ERROR);
//        }

        //把验证码存储在redis中
        String key = getPrefix(type) + mobile;
        log.info("验证码：" + key+"----------"+randomCode);
        redisTemplate.boundValueOps(key).set(randomCode,5, TimeUnit.MINUTES);

    }

    @Override
    public Boolean verifySmsCode(String mobile, String type, String checkcode) {
        if(mobile == null  || type == null || checkcode == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //获取redis中验证码
        String key = getPrefix(type) + mobile;
        String redisCode = (String) redisTemplate.boundValueOps(key).get();

        if(redisCode != null && redisCode.equals(checkcode)){
            return true;
        }
        return false;
    }

    public String getPrefix(String type){
        switch (type){
            case SMSConstant.SMS_TYPE_LOGIN:
                return SMSConstant.SMS_LOGIN_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_REGISTER:
                return SMSConstant.SMS_REGISTER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:
                return SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD:
                return SMSConstant.SMS_RESET_KEY_PREFIX;
            default:
                return null;
        }
    }
}
